var editrecord;
var slectedrecord;
var RoleRecord = Ext.data.Record.create([ 
       {name: 'programid', mapping: 'programid',type:'int'},
       {name: 'programname', mapping: 'programname'},
       {name: 'resource', mapping: 'resource'},
       {name: 'param', mapping: 'param'},
       'programid'
   ]);
   var store = new Ext.data.JsonStore({
    root: 'rows',
    totalProperty: 'total',
    idProperty: 'programid',
    fields: [
    	{name:'programid',type:'int'},
        'programname', 'resource','param'
    ],
	 url: form.contextpath + '/data/load/programlist.do'
});
 var grid = new Ext.grid.GridPanel({
    title:'功能列表',
    store: store,
    trackMouseOver:false,
    loadMask: true,
    columns:[{
        id: 'programid', 
        header: "功能Id",
        dataIndex: 'programid',
        width: 100,
        hidden: true,
        sortable: true
    },{
        header: "功能名称",
        dataIndex: 'programname',
        width: 220,
        sortable: false
    },{
        header: "资源",
        dataIndex: 'resource',
        width: 270,
        sortable: false
    },{
        header: "参数",
        dataIndex: 'param',
        width: 270,
        sortable: false
    }],
	sm: new Ext.grid.RowSelectionModel({
                singleSelect: true,
                listeners: {
                    rowselect: function(sm, row, rec) {
                    	slectedrecord = rec;
                      //  Ext.getCmp("company-form").getForm().loadRecord(rec);
                    }
                }
            }),
    bbar: new Ext.PagingToolbar({
        pageSize: 25,
        store: store,
        displayInfo: true,
        displayMsg: '显示记录 {0} - {1} / {2}',
        emptyMsg: "没有要显示的记录"
    }),
    tbar: new Ext.Toolbar({
       items:[
       		{
       			text:"添加",
       			handler:uf_add
       		},
       		{
       			text:"修改",
       			handler:uf_mod
       		},
       		'-',
       		{
       			text:"删除",
       			handler:uf_del
       		}
       ]
    })
});
 
form.onload=function() {
	this.parentPanel.add(grid);
	grid.render("content_div");
	store.load({params:{start:0, limit:25}});
};
form.destroy = function() {
	if(!roleForm){
		roleForm.destroy();
	}
	if(win){
		win.destroy();
	}
	grid.destroy();
};
var win;
var roleForm = new Ext.FormPanel({
	url : form.contextpath + "/function/saveFunc.do",
	deferredRender:false,
	border:false,
	items : {
		columnWidth: 0.4,
		xtype: 'fieldset',
		labelWidth: 60,
		defaults: {width: 320, border:false},  
		defaultType: 'textfield',
		autoHeight: true,
		border: false,
		style: {
			"margin-top": "10px" 
		},
		items: [
		{fieldLabel: '功能Id',name: 'programid',hidden:true}
		,{fieldLabel: 'oper',name: 'oper',hidden:true,	value:""	}
		,{
			fieldLabel: '功能名称',
			name: 'programname',
			listeners : {
				"change" : function() {
					roleForm.getForm().updateRecord(editrecord);
				}
			}
		},{
			fieldLabel: '资源',
			name: 'resource',
			listeners : {
				"change" : function() {
					roleForm.getForm().updateRecord(editrecord);
				}
			}
		},{
			fieldLabel: '参数',
			name: 'param',
			listeners : {
				"change" : function() {
					roleForm.getForm().updateRecord(editrecord);
				}
			}
		}]
	}
});
function createWin() {
	 if(!win){
        win = new Ext.Window({
			title:"功能信息维护",
            layout:'fit',
            width:450,
            height:200,
            closeAction:'hide',
			modal:true,
            plain: true,
            items: roleForm,
            buttons: [{
                text:'保存',
                handler: function(btn){
					if (!editrecord.modified) {
						win.hide();
						return;
					}
					btn.disable();
					roleForm.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在修改数据,请稍候...',
						success : function(form,action) {
							btn.enable();
							if (slectedrecord == null || editrecord.get("programid") !=slectedrecord.get("programid") ) {
								editrecord.set("programid",action.result.result.programid);
								editrecord.commit();
								store.insert(0,editrecord);
							} else {
								slectedrecord.set("programid",editrecord.get("programid"));
								slectedrecord.set("programname",editrecord.get("programname"));
								slectedrecord.set("resource",editrecord.get("resource"));
								slectedrecord.set("param",editrecord.get("param"));
								slectedrecord.commit();
							}
							win.hide();
						},
						failure : function(form, action) {
							Ext.Msg.show({
								title : '错误提示',
								msg : action.result.contentTypeIsValid ? '操作失败' : '操作失败,文件类型不正确!',
								buttons : Ext.Msg.OK,
								fn : function() {btn.enable();},
								icon : Ext.Msg.ERROR
							});
							win.hide();
						}
					});
                }
            },{
                text: '取消',
                handler: function(){
                    win.hide();
                }
            }],
				listeners:{
				"show" : function() {
				roleForm.getForm().loadRecord(editrecord);
			}
			}
        });
    }
}

function uf_add() {
      	editrecord = new RoleRecord({'programid':-1, 'programname':'', 'resource':'','param':''},-1);
		roleForm.getForm().setValues({"oper":"add"});
		createWin();
		win.show(this);
}

function uf_mod() {
	if (slectedrecord)	{
		editrecord= slectedrecord.copy();
		roleForm.getForm().setValues({"oper":"edit"});
		createWin();
		win.show(this);
	} else {
		Ext.MessageBox.alert("信息","请选择一行记录！");
	}
}

function uf_del() {
	if (!slectedrecord)	{
		Ext.MessageBox.alert("信息","请选择一行记录！");
		return;
	}
	Ext.MessageBox.confirm("确认信息","确定要删除该角色吗？",
	function(a1,a2,a3) {
		if (a1 == "yes") {
			Ext.Ajax.request({
				   url: form.contextpath + "/function/saveFunc.do",
				   success: function(response,opts){
					   store.remove(slectedrecord);
				   },
				   failure:  function(response,opts){
					   Ext.Msg.show({title : '错误提示',msg : '删除失败',buttons : Ext.Msg.OK,icon : Ext.Msg.ERROR});
				   },
				   params: { "oper": "del","programid":slectedrecord.get("programid") }
				});
		}
	}
	);
}

